// packageUser/pages/auth/auth-rules.js
import { throttle } from '../../../utils/util.js';
import UserService from '../../../service/UserService.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productTypes: app.globalData.productTypes,
    user: app.globalData.user
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.userService = new UserService();
    console.log(app.globalData.user);
  },
  lookProtocol: function (e) {
    const type = e.target.dataset.type;
    throttle(function () {
      wx.navigateTo({
        url: '/packageUser/pages/protocol/protocol?type=' + type
      })
    }, 1000)()
  },
  validForm: function(form){
    if(!form.productType){
      this.setData({ errMsg: '工厂分类说明不能为空' })
      return false;
    }
    if (!form.factoryNote) {
      this.setData({ errMsg: '工厂说明不能为空' })
      return false;
    }
    if (!form.cpFiles || form.cpFiles.length < 3){
      this.setData({ errMsg: '产品图片至少上传3张' })
      return false;
    }
    if (form.cpFiles && form.cpFiles.length > 9) {
      this.setData({ errMsg: '产品图片至多上传9张' })
      return false;
    }
    if (!form.ysFiles || form.ysFiles.length < 3) {
      this.setData({ errMsg: '优势图片至少上传3张' })
      return false;
    }
    if (form.ysFiles && form.ysFiles.length > 9) {
      this.setData({ errMsg: '优势图片至多上传9张' })
      return false;
    }
    if (!form.isSecurity) {
      this.setData({ errMsg: '请阅读担保交易协议' })
      return false;
    }
    if (!form.isService) {
      this.setData({ errMsg: '请阅读付款方式协议' })
      return false;
    }
    this.setData({ errMsg: '' })
    return true;
  },
  formSubmit: function (e) {
    const {user} = this.data;
    if (!this.validForm(user)){
      return;
    }
    const param = {
      id: user.id,
      factoryName: user.factoryName,
      factoryNote: user.factoryNote,
      factoryType: user.factoryType,
      cpFiles: user.cpFiles,
      ysFiles: user.ysFiles,
      isSecurity: user.isSecurity,
      isService: user.isService,
      userType: 2, 
      authStatus: 0 
    };

    this.userService.updateUser(param).then(function (res) {
      console.log(res);
      if(res.status == 0){
        wx.navigateTo({
          url: '/packageUser/pages/auth/factory-auth'
        })
      }else{
        wx.showToast({
          title: res.message,
        })
      }
    });
  },
  setFieldValue: function (field, value) {
    const { user } = this.data;
    user[field] = value;
    this.setData({ user });
  },
  onProductTypeChange: function(e){
    this.setFieldValue('productType', e.detail.value);
  },
  onFactoryNoteChange: function(e){
    this.setFieldValue('factoryNote', e.detail.value);
  },
  onTypeChange: function (e) {
    this.setFieldValue('factoryType', e.detail.id);
  },
  onCpImagesChange: function (e) {
    const images = e.detail.images.map(fileUrl => {
      return { fileUrl, fileType: 5 };
    });
    this.setFieldValue('cpFiles', images);
  },
  onYsImagesChange: function (e) {
    const images = e.detail.images.map(fileUrl => {
      return { fileUrl, fileType: 6 };
    });
    this.setFieldValue('ysFiles', images);
  },
  onIsSecurity: function (e) {
    const { value } = e.detail;
    this.setFieldValue('isSecurity', value.join(""));
  },
  onIsService: function (e) {
    const { value } = e.detail;
    this.setFieldValue('isService', value.join(""));
  }



  
})